const tables = require("../../public/tables.js");
const random = require("../get/random.js");
const string=require("../get/string.js");
const source = require("../../public/source.js");
const Service = require('egg').Service;
class TableService extends Service {
    fieldsStr(tabName) {
        return tables.filter(table => table.name == tabName)[0].fields.toString();
    }
    valuesStr(tabName, total) {
        let values = [];
        let fields = tables.filter((table, ind) => table.name == tabName)[0].fields;
        for (let i = 0; i < total; i++) {
            let vals = [];
            fields.forEach(field => {
                vals.push(this.service.get.fileds[tabName][field](i, total));
            })
            values.push(`(${vals.toString()})`);
        }
        return values.toString();
    }
    async initData(currTabInfo, currTabMeth, total) {
        // let currTabInfo = tables.filter((table, ind) => table.name == tabName)[0];
        let { fields, foreigns } = currTabInfo;
        let fieldsStr = fields.toString(), valuesStr = "";
        let values = [];
        let foreignArr = [];
        let array = [];
        if (source.regexp.areaStr.test(total)) {   //判断total是否是区间数组（字符串形式）
            total = random.numByArea(total);
        } else if (source.regexp.weightStr.test(total)) {
            total = random.numByWeight(string.toWeightArr(total));
        }
        for (let i = 0; i < total; i++) {
            let vals = [];
            //下面不能使用foreach,因为使用foreach,则await 后面的方法都是并发操作的，会出问题(踏过的很大的坑)
            for (let j = 0; j < fields.length; j++) {
                let val = await currTabMeth[fields[j]](i, total);
                vals.push(val);
            }
            if(!vals.every(item=>item===null)){
                values.push(`(${vals.toString()})`);
            }
            valuesStr = values.toString();
            if (foreigns.length > 0) {
                for (let i = 0; i < foreigns.length; i++) {
                    let { name, total } = foreigns[i];
                    let tabName = this.service.utility.toWriteMode(name, "modeType");
                    let foreignObj = await this.initData(foreigns[i], currTabMeth.foreigns[tabName], total);
                    array[i] ? array[i].push(foreignObj.valuesStr) : array.push([foreignObj.valuesStr]);
                }
            }
        }
        if (foreigns.length > 0) {
            for (let i = 0; i < foreigns.length; i++) {
                let { name, total } = foreigns[i];
                let tabName = this.service.utility.toWriteMode(name, "modeType");
                let foreignObj = await this.initData(foreigns[i], currTabMeth.foreigns[tabName], total);
                foreignObj.valuesStr = array[i].toString();
                foreignArr.push(foreignObj);
            }
        }
        return { tabName: currTabInfo.name, fieldsStr, valuesStr, foreigns: foreignArr };
    }
}
module.exports = TableService;
